{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "9a71fa36",
   "metadata": {},
   "source": [
    "# ReAct\n",
    "\n",
    "AutoGen supports different LLM prompting and reasoning strategies, such as ReAct, Reflection/Self-Critique, and more. This page demonstrates how to realize ReAct ([Yao et al., 2022](https://arxiv.org/abs/2210.03629)) with AutoGen. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "2b803c17",
   "metadata": {},
   "outputs": [],
   "source": [
    "# %pip install \"pyautogen>=0.2.18\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dca301a4",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_community.tools.tavily_search import TavilySearchResults\n",
    "from langchain.utilities.tavily_search import TavilySearchAPIWrapper\n",
    "import autogen\n",
    "\n",
    "config_list = autogen.config_list_from_json(\n",
    "    \"OAI_CONFIG_LIST\",\n",
    ")\n",
    "%env TAVILY_API_KEY=\"YOUR_API_KEY\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0ae63ce7",
   "metadata": {},
   "source": [
    "### Define and add tools/actions you want LLM agent(s) to use"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5bdce8c0",
   "metadata": {},
   "outputs": [],
   "source": [
    "search = TavilySearchAPIWrapper()\n",
    "tavily_tool = TavilySearchResults(api_wrapper=search)\n",
    "search_tool = TavilySearchResults()\n",
    "\n",
    "\n",
    "def generate_llm_config(tool):\n",
    "    # Define the function schema based on the tool's args_schema\n",
    "    function_schema = {\n",
    "        \"name\": tool.name.lower().replace(\" \", \"_\"),\n",
    "        \"description\": tool.description,\n",
    "        \"parameters\": {\n",
    "            \"type\": \"object\",\n",
    "            \"properties\": {},\n",
    "            \"required\": [],\n",
    "        },\n",
    "    }\n",
    "\n",
    "    if tool.args is not None:\n",
    "        function_schema[\"parameters\"][\"properties\"] = tool.args\n",
    "\n",
    "    return function_schema\n",
    "\n",
    "\n",
    "# Construct the llm_config\n",
    "llm_config = {\n",
    "    # Generate functions config for the Tool\n",
    "    \"functions\": [\n",
    "        generate_llm_config(search_tool),\n",
    "    ],\n",
    "    \"config_list\": config_list,  # Assuming you have this defined elsewhere\n",
    "    \"timeout\": 120,\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "af2911f3",
   "metadata": {},
   "source": [
    "### Construct your ReAct prompt\n",
    "Here we are constructing a general ReAct prompt and a custom message function `react_prompt_message` based on the ReAct prompt."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6823ae2e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# NOTE: this ReAct prompt is adapted from Langchain's ReAct agent: https://github.com/langchain-ai/langchain/blob/master/libs/langchain/langchain/agents/react/agent.py#L79\n",
    "ReAct_prompt = \"\"\"\n",
    "Answer the following questions as best you can. You have access to tools provided.\n",
    "\n",
    "Use the following format:\n",
    "\n",
    "Question: the input question you must answer\n",
    "Thought: you should always think about what to do\n",
    "Action: the action to take\n",
    "Action Input: the input to the action\n",
    "Observation: the result of the action\n",
    "... (this process can repeat multiple times)\n",
    "Thought: I now know the final answer\n",
    "Final Answer: the final answer to the original input question\n",
    "\n",
    "Begin!\n",
    "Question: {input}\n",
    "\"\"\"\n",
    "\n",
    "# Define the ReAct prompt message. Assuming a \"question\" field is present in the context\n",
    "\n",
    "\n",
    "def react_prompt_message(sender, recipient, context):\n",
    "    return ReAct_prompt.format(input=context[\"question\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "859776fd",
   "metadata": {},
   "source": [
    "### Construct agents and initiate agent chats"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "d8d45113",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[33muser_proxy\u001b[0m (to Assistant):\n",
      "\n",
      "\n",
      "Answer the following questions as best you can. You have access to tools provided.\n",
      "\n",
      "Use the following format:\n",
      "\n",
      "Question: the input question you must answer\n",
      "Thought: you should always think about what to do\n",
      "Action: the action to take\n",
      "Action Input: the input to the action\n",
      "Observation: the result of the action\n",
      "... (this process can repeat multiple times)\n",
      "Thought: I now know the final answer\n",
      "Final Answer: the final answer to the original input question\n",
      "\n",
      "Begin!\n",
      "Question: What is the result of super bowl 2024?\n",
      "\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[33mAssistant\u001b[0m (to user_proxy):\n",
      "\n",
      "Thought: I need to find the most current information on the result of the Super Bowl 2024.\n",
      "Action: Use the search tool provided to find the result.\n",
      "Action Input: query = \"Super Bowl 2024 result\"\n",
      "\n",
      "\u001b[32m***** Suggested function Call: tavily_search_results_json *****\u001b[0m\n",
      "Arguments: \n",
      "{\"query\":\"Super Bowl 2024 result\"}\n",
      "\u001b[32m***************************************************************\u001b[0m\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[35m\n",
      ">>>>>>>> EXECUTING FUNCTION tavily_search_results_json...\u001b[0m\n",
      "\u001b[33muser_proxy\u001b[0m (to Assistant):\n",
      "\n",
      "\u001b[32m***** Response from calling function \"tavily_search_results_json\" *****\u001b[0m\n",
      "[{'url': 'https://www.sportingnews.com/us/nfl/news/super-bowl-2024-live-score-49ers-chiefs-results-highlights/0c440aa7145b809ed174d8ff', 'content': 'Super Bowl start time\\nSuper Bowl 58 between the Chiefs and 49ers is set to kick off at 6:30 p.m. ET (3:30 p.m. local time) in Las Vegas, Nev.\\n6:30 p.m. ET has become the standard start time for Super Bowls and is preceded by performances of \"Lift Every Voice and Sing,\" \"America the Beautiful,\" and \"The Star-Spangled Banner. Brock Purdy drops a dime to Chris Conley on 3rd & 9 🎯\\n📺: #SBLVIII on CBS\\n📱: Stream on #NFLPlus https://t.co/dClcEDViWl pic.twitter.com/Oa10d7khdl\\n7:10 p.m. — The 49ers are relying heavily on McCaffrey, who takes the ball on each of the first three snaps on this drive. 📺: #SBLVIII on CBS\\n📱: Stream on #NFLPlus https://t.co/dClcEDViWl pic.twitter.com/yUc00MtP84\\n7:24 p.m. — The 49ers force a 3rd & 1, but Rashee Rice is easily able to pick up the first and one or two more yards.\\n49ers 3, Chiefs 0\\n7:19 p.m. MORE SUPER BOWL 58:\\n•\\xa0 Inside Taylor Swift and Travis Kelce\\'s whirlwind dating timeline\\n•. Ranking the thriving Mike and Kyle Shanahan coaching tree\\n•. Tracking every Super Bowl 58 commercial\\nWhat channel is the Super Bowl on?\\nSuper Bowl 58 will be broadcast nationally on CBS. PURDY TO JENNINGS TO CMC FOR SIX 🔥\\n📺: #SBLVIII on CBS\\n📱: Stream on #NFLPlus https://t.co/dClcEDViWl pic.twitter.com/ktiTXIiHzS\\n7:47 p.m. — L\\'Jarius Sneed, who was on the other side of this two weeks ago, gets hit with an unsportsmanlike conduct penalty and and gives the 49ers their 11th first down.'}, {'url': 'https://www.cbssports.com/nfl/news/2024-super-bowl-chiefs-vs-49ers-score-patrick-mahomes-leads-ot-comeback-as-k-c-wins-back-to-back-titles/live/', 'content': \"The championship-winning drive, which included a fourth-and-1 scramble from Mahomes and a clutch 7-yard catch from tight end Travis Kelce, was a must-score for K.C. The NFL's new playoff overtime rules -- both teams are guaranteed at least one possession in the extra period -- were in effect for the first time, and the Chiefs needed to answer the Niners' field goal.\\n Held out of the end zone until that point, Kansas City grabbed its first lead of the game at 13-10.\\nJennings' touchdown receiving (followed by a missed extra point) concluded a 75-yard drive that put the Niners back on top, 16-13, as the wideout joined former Philadelphia Eagles quarterback Nick Foles as the only players to throw and catch a touchdown in a Super Bowl.\\n He spread the ball around -- eight pass-catchers had at least two receptions -- slowly but surely overcoming a threatening 49ers defense that knocked him off his spot consistently in the first half.\\nMahomes, with his third Super Bowl MVP, now sits alongside Tom Brady (five) and Joe Montana (three) atop the mountain while becoming just the third player to win the award back-to-back, joining Bart Starr (I-II) and Terry Bradshaw (XIII-XIV).\\n The muffed punt that bounced off of cornerback Darrell Luter Jr.'s ankle was also the big break that the Chiefs needed as they scored on the very next play to take the lead for the first time in the game. College Pick'em\\nA Daily SportsLine Betting Podcast\\nNFL Playoff Time!\\n2024 Super Bowl, Chiefs vs. 49ers score: Patrick Mahomes leads OT comeback as K.C. wins back-to-back titles\\nCall it a dynasty; the Chiefs are the first team to win consecutive Super Bowls since 2003-04\\nThe Kansas City Chiefs are Super Bowl champions, again.\"}, {'url': 'https://www.espn.com/nfl/story/_/id/39480722/49ers-chiefs-live-super-bowl-lviii-updates-moments-highlights', 'content': \"With a relentless defense and opportune plays by their star quarterback -- including a pair of gutsy overtime scrambles -- the Chiefs won their third Super Bowl in five years in a 25-22 overtime victory against the San Francisco 49ers in only the second overtime game in Super Bowl history.\\n Staff\\nTaylor Swift supports Travis Kelce, chugs drink at Super Bowl LVIII10hTory Barron\\nAfter posting a losing record in December, the Chiefs embraced an underdog, villain mentality throughout the postseason, upsetting three teams en route to their second consecutive Super Bowl title, becoming the ninth team to repeat as Super Bowl champions and first since the 2004 New England Patriots.\\n ESPN\\nSuper Bowl 2024 - Highlights from Chiefs' win vs. 49ers\\nMike Tannenbaum and Tim Hasselbeck react to the Chiefs' thrilling overtime victory over the 49ers in the Super Bowl. The 49ers had the ball with 2:51 to go in a tied game, but a signature Steve Spagnuolo blitz on third down late in the fourth quarter forced a 49ers field goal. Led by their captains, most of the Chiefs arrived to Allegiant Stadium in Las Vegas on Sunday in all black, signaling a steely resolve to upset Brock Purdy and the NFC's best offensive ensemble.\\n\"}, {'url': 'https://www.nbcnews.com/news/sports/live-blog/super-bowl-chiefs-49ers-score-live-updates-rcna136833', 'content': \"Profile\\nSections\\ntv\\nFeatured\\nMore From NBC\\nFollow NBC News\\nnews Alerts\\nThere are no new alerts at this time\\nSuper Bowl LVIII\\nSuper Bowl 2024 live updates: 49ers vs. Chiefs how to watch, kickoff time, pregame, Taylor Swift arrival\\nEverything you need to know about Super Bowl 58:\\nJason Abbruzzese\\nAnd we're under way. “Despite the 12-hour flight and 17-hour time difference, the Embassy can confidently Speak Now to say that if she departs Tokyo in the evening after her concert, she should comfortably arrive in Las Vegas before the Super Bowl begins.”\\nNBC News\\nFootball fans across the country were celebrating after they were surprised with tickets to the Super Bowl in Las Vegas. Mahomes warms up\\nNBC News\\nICYMI: A person tried to climb the Vegas Sphere (and got arrested)\\nSaba Hamedy\\nLas Vegas police confirmed last Wednesday that a man was arrested after he climbed a structure on the 200 block of Sands Avenue, where the Sphere is located.\\n President Biden is skipping a Super Bowl interview\\nMonica Alba\\nJonathan Allen\\nFor the second year in a row, President Joe Biden is passing on the opportunity to sit down for a Super Bowl interview that could reach millions of Americans on Sunday — a move his advisers say is part of their larger communication strategy.\\n Can the Niners tie the Patriots and Steelers?\\nClaire Cardona\\nAllan Smith\\nThe Kansas City Chiefs, the reigning Super Bowl champs, could go two in a row if they beat the San Francisco 49ers.\\n\"}, {'url': 'https://apnews.com/live/super-bowl-2024-updates', 'content': 'Throw in the fact that Chiefs coach Andy Reid will be in his fifth Super Bowl, the third most in NFL history, and has a chance to win a third ring, and the knowledge on the Kansas City sideline will be an advantage too big for the 49ers to overcome.\\n She performed in Japan on Saturday night before a flight across nine time zones and the international date line to reach the U.S.\\nRihanna performs during halftime of the NFL Super Bowl 57 football game between the Philadelphia Eagles and the Kansas City Chiefs, Sunday, Feb. 12, 2023, in Glendale, Ariz. (AP Photo/David J. Phillip)\\n After the teams take the field, Post Malone will perform “America the Beautiful” and Reba McEntire will sing “The Star-Spangled Banner.”\\nSan Francisco 49ers quarterback Brock Purdy (13) warms up before the NFL Super Bowl 58 football game against the Kansas City Chiefs, Sunday, Feb. 11, 2024, in Las Vegas. He was also the referee when the Chiefs beat the 49ers in the Super Bowl four years ago — and when the Rams beat the Saints in the 2019 NFC championship game after an infamous missed call.\\n Purdy’s comeback from the injury to his throwing arm suffered in last season’s NFC championship loss to the Philadelphia Eagles has been part of the storybook start to his career that started as Mr. Irrelevant as the 262nd pick in the 2022 draft.\\n'}]\n",
      "\u001b[32m***********************************************************************\u001b[0m\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[33mAssistant\u001b[0m (to user_proxy):\n",
      "\n",
      "Thought: The information provided indicates that the Kansas City Chiefs won Super Bowl 2024 against the San Francisco 49ers with a score of 25-22 in overtime, led by Patrick Mahomes, who earned his third Super Bowl MVP.\n",
      "\n",
      "Final Answer: The Kansas City Chiefs won Super Bowl 2024 against the San Francisco 49ers with a final score of 25-22 in overtime.\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[33muser_proxy\u001b[0m (to Assistant):\n",
      "\n",
      "\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[33mAssistant\u001b[0m (to user_proxy):\n",
      "\n",
      "TERMINATE\n",
      "\n",
      "--------------------------------------------------------------------------------\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "ChatResult(chat_id=None, chat_history=[{'content': '\\nAnswer the following questions as best you can. You have access to tools provided.\\n\\nUse the following format:\\n\\nQuestion: the input question you must answer\\nThought: you should always think about what to do\\nAction: the action to take\\nAction Input: the input to the action\\nObservation: the result of the action\\n... (this process can repeat multiple times)\\nThought: I now know the final answer\\nFinal Answer: the final answer to the original input question\\n\\nBegin!\\nQuestion: What is the result of super bowl 2024?\\n', 'role': 'assistant'}, {'content': 'Thought: I need to find the most current information on the result of the Super Bowl 2024.\\nAction: Use the search tool provided to find the result.\\nAction Input: query = \"Super Bowl 2024 result\"\\n', 'function_call': {'arguments': '{\"query\":\"Super Bowl 2024 result\"}', 'name': 'tavily_search_results_json'}, 'role': 'assistant'}, {'content': '[{\\'url\\': \\'https://www.sportingnews.com/us/nfl/news/super-bowl-2024-live-score-49ers-chiefs-results-highlights/0c440aa7145b809ed174d8ff\\', \\'content\\': \\'Super Bowl start time\\\\nSuper Bowl 58 between the Chiefs and 49ers is set to kick off at 6:30 p.m. ET (3:30 p.m. local time) in Las Vegas, Nev.\\\\n6:30 p.m. ET has become the standard start time for Super Bowls and is preceded by performances of \"Lift Every Voice and Sing,\" \"America the Beautiful,\" and \"The Star-Spangled Banner. Brock Purdy drops a dime to Chris Conley on 3rd & 9 🎯\\\\n📺: #SBLVIII on CBS\\\\n📱: Stream on #NFLPlus https://t.co/dClcEDViWl pic.twitter.com/Oa10d7khdl\\\\n7:10 p.m. — The 49ers are relying heavily on McCaffrey, who takes the ball on each of the first three snaps on this drive. 📺: #SBLVIII on CBS\\\\n📱: Stream on #NFLPlus https://t.co/dClcEDViWl pic.twitter.com/yUc00MtP84\\\\n7:24 p.m. — The 49ers force a 3rd & 1, but Rashee Rice is easily able to pick up the first and one or two more yards.\\\\n49ers 3, Chiefs 0\\\\n7:19 p.m. MORE SUPER BOWL 58:\\\\n•\\\\xa0 Inside Taylor Swift and Travis Kelce\\\\\\'s whirlwind dating timeline\\\\n•. Ranking the thriving Mike and Kyle Shanahan coaching tree\\\\n•. Tracking every Super Bowl 58 commercial\\\\nWhat channel is the Super Bowl on?\\\\nSuper Bowl 58 will be broadcast nationally on CBS. PURDY TO JENNINGS TO CMC FOR SIX 🔥\\\\n📺: #SBLVIII on CBS\\\\n📱: Stream on #NFLPlus https://t.co/dClcEDViWl pic.twitter.com/ktiTXIiHzS\\\\n7:47 p.m. — L\\\\\\'Jarius Sneed, who was on the other side of this two weeks ago, gets hit with an unsportsmanlike conduct penalty and and gives the 49ers their 11th first down.\\'}, {\\'url\\': \\'https://www.cbssports.com/nfl/news/2024-super-bowl-chiefs-vs-49ers-score-patrick-mahomes-leads-ot-comeback-as-k-c-wins-back-to-back-titles/live/\\', \\'content\\': \"The championship-winning drive, which included a fourth-and-1 scramble from Mahomes and a clutch 7-yard catch from tight end Travis Kelce, was a must-score for K.C. The NFL\\'s new playoff overtime rules -- both teams are guaranteed at least one possession in the extra period -- were in effect for the first time, and the Chiefs needed to answer the Niners\\' field goal.\\\\n Held out of the end zone until that point, Kansas City grabbed its first lead of the game at 13-10.\\\\nJennings\\' touchdown receiving (followed by a missed extra point) concluded a 75-yard drive that put the Niners back on top, 16-13, as the wideout joined former Philadelphia Eagles quarterback Nick Foles as the only players to throw and catch a touchdown in a Super Bowl.\\\\n He spread the ball around -- eight pass-catchers had at least two receptions -- slowly but surely overcoming a threatening 49ers defense that knocked him off his spot consistently in the first half.\\\\nMahomes, with his third Super Bowl MVP, now sits alongside Tom Brady (five) and Joe Montana (three) atop the mountain while becoming just the third player to win the award back-to-back, joining Bart Starr (I-II) and Terry Bradshaw (XIII-XIV).\\\\n The muffed punt that bounced off of cornerback Darrell Luter Jr.\\'s ankle was also the big break that the Chiefs needed as they scored on the very next play to take the lead for the first time in the game. College Pick\\'em\\\\nA Daily SportsLine Betting Podcast\\\\nNFL Playoff Time!\\\\n2024 Super Bowl, Chiefs vs. 49ers score: Patrick Mahomes leads OT comeback as K.C. wins back-to-back titles\\\\nCall it a dynasty; the Chiefs are the first team to win consecutive Super Bowls since 2003-04\\\\nThe Kansas City Chiefs are Super Bowl champions, again.\"}, {\\'url\\': \\'https://www.espn.com/nfl/story/_/id/39480722/49ers-chiefs-live-super-bowl-lviii-updates-moments-highlights\\', \\'content\\': \"With a relentless defense and opportune plays by their star quarterback -- including a pair of gutsy overtime scrambles -- the Chiefs won their third Super Bowl in five years in a 25-22 overtime victory against the San Francisco 49ers in only the second overtime game in Super Bowl history.\\\\n Staff\\\\nTaylor Swift supports Travis Kelce, chugs drink at Super Bowl LVIII10hTory Barron\\\\nAfter posting a losing record in December, the Chiefs embraced an underdog, villain mentality throughout the postseason, upsetting three teams en route to their second consecutive Super Bowl title, becoming the ninth team to repeat as Super Bowl champions and first since the 2004 New England Patriots.\\\\n ESPN\\\\nSuper Bowl 2024 - Highlights from Chiefs\\' win vs. 49ers\\\\nMike Tannenbaum and Tim Hasselbeck react to the Chiefs\\' thrilling overtime victory over the 49ers in the Super Bowl. The 49ers had the ball with 2:51 to go in a tied game, but a signature Steve Spagnuolo blitz on third down late in the fourth quarter forced a 49ers field goal. Led by their captains, most of the Chiefs arrived to Allegiant Stadium in Las Vegas on Sunday in all black, signaling a steely resolve to upset Brock Purdy and the NFC\\'s best offensive ensemble.\\\\n\"}, {\\'url\\': \\'https://www.nbcnews.com/news/sports/live-blog/super-bowl-chiefs-49ers-score-live-updates-rcna136833\\', \\'content\\': \"Profile\\\\nSections\\\\ntv\\\\nFeatured\\\\nMore From NBC\\\\nFollow NBC News\\\\nnews Alerts\\\\nThere are no new alerts at this time\\\\nSuper Bowl LVIII\\\\nSuper Bowl 2024 live updates: 49ers vs. Chiefs how to watch, kickoff time, pregame, Taylor Swift arrival\\\\nEverything you need to know about Super Bowl 58:\\\\nJason Abbruzzese\\\\nAnd we\\'re under way. “Despite the 12-hour flight and 17-hour time difference, the Embassy can confidently Speak Now to say that if she departs Tokyo in the evening after her concert, she should comfortably arrive in Las Vegas before the Super Bowl begins.”\\\\nNBC News\\\\nFootball fans across the country were celebrating after they were surprised with tickets to the Super Bowl in Las Vegas. Mahomes warms up\\\\nNBC News\\\\nICYMI: A person tried to climb the Vegas Sphere (and got arrested)\\\\nSaba Hamedy\\\\nLas Vegas police confirmed last Wednesday that a man was arrested after he climbed a structure on the 200 block of Sands Avenue, where the Sphere is located.\\\\n President Biden is skipping a Super Bowl interview\\\\nMonica Alba\\\\nJonathan Allen\\\\nFor the second year in a row, President Joe Biden is passing on the opportunity to sit down for a Super Bowl interview that could reach millions of Americans on Sunday — a move his advisers say is part of their larger communication strategy.\\\\n Can the Niners tie the Patriots and Steelers?\\\\nClaire Cardona\\\\nAllan Smith\\\\nThe Kansas City Chiefs, the reigning Super Bowl champs, could go two in a row if they beat the San Francisco 49ers.\\\\n\"}, {\\'url\\': \\'https://apnews.com/live/super-bowl-2024-updates\\', \\'content\\': \\'Throw in the fact that Chiefs coach Andy Reid will be in his fifth Super Bowl, the third most in NFL history, and has a chance to win a third ring, and the knowledge on the Kansas City sideline will be an advantage too big for the 49ers to overcome.\\\\n She performed in Japan on Saturday night before a flight across nine time zones and the international date line to reach the U.S.\\\\nRihanna performs during halftime of the NFL Super Bowl 57 football game between the Philadelphia Eagles and the Kansas City Chiefs, Sunday, Feb. 12, 2023, in Glendale, Ariz. (AP Photo/David J. Phillip)\\\\n After the teams take the field, Post Malone will perform “America the Beautiful” and Reba McEntire will sing “The Star-Spangled Banner.”\\\\nSan Francisco 49ers quarterback Brock Purdy (13) warms up before the NFL Super Bowl 58 football game against the Kansas City Chiefs, Sunday, Feb. 11, 2024, in Las Vegas. He was also the referee when the Chiefs beat the 49ers in the Super Bowl four years ago — and when the Rams beat the Saints in the 2019 NFC championship game after an infamous missed call.\\\\n Purdy’s comeback from the injury to his throwing arm suffered in last season’s NFC championship loss to the Philadelphia Eagles has been part of the storybook start to his career that started as Mr. Irrelevant as the 262nd pick in the 2022 draft.\\\\n\\'}]', 'name': 'tavily_search_results_json', 'role': 'function'}, {'content': 'Thought: The information provided indicates that the Kansas City Chiefs won Super Bowl 2024 against the San Francisco 49ers with a score of 25-22 in overtime, led by Patrick Mahomes, who earned his third Super Bowl MVP.\\n\\nFinal Answer: The Kansas City Chiefs won Super Bowl 2024 against the San Francisco 49ers with a final score of 25-22 in overtime.', 'role': 'user'}, {'content': '', 'role': 'assistant'}, {'content': 'TERMINATE', 'role': 'user'}], summary='', cost=({'total_cost': 0.15722999999999998, 'gpt-4': {'cost': 0.15722999999999998, 'prompt_tokens': 4929, 'completion_tokens': 156, 'total_tokens': 5085}}, {'total_cost': 0.14655, 'gpt-4': {'cost': 0.14655, 'prompt_tokens': 4713, 'completion_tokens': 86, 'total_tokens': 4799}}), human_input=[])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_proxy = autogen.UserProxyAgent(\n",
    "    name=\"user_proxy\",\n",
    "    is_termination_msg=lambda x: x.get(\"content\", \"\") and x.get(\"content\", \"\").rstrip().endswith(\"TERMINATE\"),\n",
    "    human_input_mode=\"NEVER\",\n",
    "    max_consecutive_auto_reply=10,\n",
    "    code_execution_config={\n",
    "        # \"work_dir\": \"coding\",\n",
    "        \"use_docker\": False,\n",
    "    },  # Please set use_docker=True if docker is available to run the generated code. Using docker is safer than running the generated code directly.\n",
    ")\n",
    "\n",
    "# Register the tool and start the conversation\n",
    "user_proxy.register_function(\n",
    "    function_map={\n",
    "        search_tool.name: search_tool._run,\n",
    "    }\n",
    ")\n",
    "\n",
    "assistant = autogen.AssistantAgent(\n",
    "    name=\"Assistant\",\n",
    "    system_message=\"Only use the tools you have been provided with. Reply TERMINATE when the task is done.\",\n",
    "    llm_config=llm_config,\n",
    ")\n",
    "\n",
    "user_proxy.initiate_chat(\n",
    "    assistant,\n",
    "    message=react_prompt_message,\n",
    "    question=\"What is the result of super bowl 2024?\",\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f203a72f",
   "metadata": {},
   "source": [
    "### ReAct with memory module enabled via teachability"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "23d47514",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[92m\n",
      "CLEARING MEMORY\u001b[0m\n",
      "\u001b[33muser_proxy\u001b[0m (to Assistant):\n",
      "\n",
      "\n",
      "Answer the following questions as best you can. You have access to tools provided.\n",
      "\n",
      "Use the following format:\n",
      "\n",
      "Question: the input question you must answer\n",
      "Thought: you should always think about what to do\n",
      "Action: the action to take\n",
      "Action Input: the input to the action\n",
      "Observation: the result of the action\n",
      "... (this process can repeat multiple times)\n",
      "Thought: I now know the final answer\n",
      "Final Answer: the final answer to the original input question\n",
      "\n",
      "Begin!\n",
      "Question: What is the result of super bowl 2024?\n",
      "\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[33mAssistant\u001b[0m (to user_proxy):\n",
      "\n",
      "Thought: To find the result of Super Bowl 2024, I need to search for the most recent information available since the event is in the future as of my knowledge cutoff, and the result would only be known after the event takes place.\n",
      "\n",
      "Action: Use the provided search function to find the result of Super Bowl 2024.\n",
      "Action Input: { query: \"Super Bowl 2024 result\" }\n",
      "\u001b[32m***** Suggested function Call: tavily_search_results_json *****\u001b[0m\n",
      "Arguments: \n",
      "{\"query\":\"Super Bowl 2024 result\"}\n",
      "\u001b[32m***************************************************************\u001b[0m\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[35m\n",
      ">>>>>>>> EXECUTING FUNCTION tavily_search_results_json...\u001b[0m\n",
      "\u001b[33muser_proxy\u001b[0m (to Assistant):\n",
      "\n",
      "\u001b[32m***** Response from calling function \"tavily_search_results_json\" *****\u001b[0m\n",
      "[{'url': 'https://www.espn.com/nfl/story/_/id/39480722/49ers-chiefs-live-super-bowl-lviii-updates-moments-highlights', 'content': \"With a relentless defense and opportune plays by their star quarterback -- including a pair of gutsy overtime scrambles -- the Chiefs won their third Super Bowl in five years in a 25-22 overtime victory against the San Francisco 49ers in only the second overtime game in Super Bowl history.\\n Staff\\nTaylor Swift supports Travis Kelce, chugs drink at Super Bowl LVIII10hTory Barron\\nAfter posting a losing record in December, the Chiefs embraced an underdog, villain mentality throughout the postseason, upsetting three teams en route to their second consecutive Super Bowl title, becoming the ninth team to repeat as Super Bowl champions and first since the 2004 New England Patriots.\\n ESPN\\nSuper Bowl 2024 - Highlights from Chiefs' win vs. 49ers\\nMike Tannenbaum and Tim Hasselbeck react to the Chiefs' thrilling overtime victory over the 49ers in the Super Bowl. The 49ers had the ball with 2:51 to go in a tied game, but a signature Steve Spagnuolo blitz on third down late in the fourth quarter forced a 49ers field goal. Led by their captains, most of the Chiefs arrived to Allegiant Stadium in Las Vegas on Sunday in all black, signaling a steely resolve to upset Brock Purdy and the NFC's best offensive ensemble.\\n\"}, {'url': 'https://www.sportingnews.com/us/nfl/news/super-bowl-2024-live-score-49ers-chiefs-results-highlights/0c440aa7145b809ed174d8ff', 'content': 'Super Bowl start time\\nSuper Bowl 58 between the Chiefs and 49ers is set to kick off at 6:30 p.m. ET (3:30 p.m. local time) in Las Vegas, Nev.\\n6:30 p.m. ET has become the standard start time for Super Bowls and is preceded by performances of \"Lift Every Voice and Sing,\" \"America the Beautiful,\" and \"The Star-Spangled Banner. Brock Purdy drops a dime to Chris Conley on 3rd & 9 🎯\\n📺: #SBLVIII on CBS\\n📱: Stream on #NFLPlus https://t.co/dClcEDViWl pic.twitter.com/Oa10d7khdl\\n7:10 p.m. — The 49ers are relying heavily on McCaffrey, who takes the ball on each of the first three snaps on this drive. 📺: #SBLVIII on CBS\\n📱: Stream on #NFLPlus https://t.co/dClcEDViWl pic.twitter.com/yUc00MtP84\\n7:24 p.m. — The 49ers force a 3rd & 1, but Rashee Rice is easily able to pick up the first and one or two more yards.\\n49ers 3, Chiefs 0\\n7:19 p.m. MORE SUPER BOWL 58:\\n•\\xa0 Inside Taylor Swift and Travis Kelce\\'s whirlwind dating timeline\\n•. Ranking the thriving Mike and Kyle Shanahan coaching tree\\n•. Tracking every Super Bowl 58 commercial\\nWhat channel is the Super Bowl on?\\nSuper Bowl 58 will be broadcast nationally on CBS. PURDY TO JENNINGS TO CMC FOR SIX 🔥\\n📺: #SBLVIII on CBS\\n📱: Stream on #NFLPlus https://t.co/dClcEDViWl pic.twitter.com/ktiTXIiHzS\\n7:47 p.m. — L\\'Jarius Sneed, who was on the other side of this two weeks ago, gets hit with an unsportsmanlike conduct penalty and and gives the 49ers their 11th first down.'}, {'url': 'https://www.nbcnews.com/news/sports/live-blog/super-bowl-chiefs-49ers-score-live-updates-rcna136833', 'content': \"Profile\\nSections\\ntv\\nFeatured\\nMore From NBC\\nFollow NBC News\\nnews Alerts\\nThere are no new alerts at this time\\nSuper Bowl LVIII\\nSuper Bowl 2024 live updates: 49ers vs. Chiefs how to watch, kickoff time, pregame, Taylor Swift arrival\\nEverything you need to know about Super Bowl 58:\\nJason Abbruzzese\\nAnd we're under way. “Despite the 12-hour flight and 17-hour time difference, the Embassy can confidently Speak Now to say that if she departs Tokyo in the evening after her concert, she should comfortably arrive in Las Vegas before the Super Bowl begins.”\\nNBC News\\nFootball fans across the country were celebrating after they were surprised with tickets to the Super Bowl in Las Vegas. Mahomes warms up\\nNBC News\\nICYMI: A person tried to climb the Vegas Sphere (and got arrested)\\nSaba Hamedy\\nLas Vegas police confirmed last Wednesday that a man was arrested after he climbed a structure on the 200 block of Sands Avenue, where the Sphere is located.\\n President Biden is skipping a Super Bowl interview\\nMonica Alba\\nJonathan Allen\\nFor the second year in a row, President Joe Biden is passing on the opportunity to sit down for a Super Bowl interview that could reach millions of Americans on Sunday — a move his advisers say is part of their larger communication strategy.\\n Can the Niners tie the Patriots and Steelers?\\nClaire Cardona\\nAllan Smith\\nThe Kansas City Chiefs, the reigning Super Bowl champs, could go two in a row if they beat the San Francisco 49ers.\\n\"}, {'url': 'https://www.cbssports.com/nfl/news/2024-super-bowl-chiefs-vs-49ers-score-patrick-mahomes-leads-ot-comeback-as-k-c-wins-back-to-back-titles/live/', 'content': \"The championship-winning drive, which included a fourth-and-1 scramble from Mahomes and a clutch 7-yard catch from tight end Travis Kelce, was a must-score for K.C. The NFL's new playoff overtime rules -- both teams are guaranteed at least one possession in the extra period -- were in effect for the first time, and the Chiefs needed to answer the Niners' field goal.\\n Held out of the end zone until that point, Kansas City grabbed its first lead of the game at 13-10.\\nJennings' touchdown receiving (followed by a missed extra point) concluded a 75-yard drive that put the Niners back on top, 16-13, as the wideout joined former Philadelphia Eagles quarterback Nick Foles as the only players to throw and catch a touchdown in a Super Bowl.\\n He spread the ball around -- eight pass-catchers had at least two receptions -- slowly but surely overcoming a threatening 49ers defense that knocked him off his spot consistently in the first half.\\nMahomes, with his third Super Bowl MVP, now sits alongside Tom Brady (five) and Joe Montana (three) atop the mountain while becoming just the third player to win the award back-to-back, joining Bart Starr (I-II) and Terry Bradshaw (XIII-XIV).\\n The muffed punt that bounced off of cornerback Darrell Luter Jr.'s ankle was also the big break that the Chiefs needed as they scored on the very next play to take the lead for the first time in the game. College Pick'em\\nA Daily SportsLine Betting Podcast\\nNFL Playoff Time!\\n2024 Super Bowl, Chiefs vs. 49ers score: Patrick Mahomes leads OT comeback as K.C. wins back-to-back titles\\nCall it a dynasty; the Chiefs are the first team to win consecutive Super Bowls since 2003-04\\nThe Kansas City Chiefs are Super Bowl champions, again.\"}, {'url': 'https://apnews.com/live/super-bowl-2024-updates', 'content': 'Throw in the fact that Chiefs coach Andy Reid will be in his fifth Super Bowl, the third most in NFL history, and has a chance to win a third ring, and the knowledge on the Kansas City sideline will be an advantage too big for the 49ers to overcome.\\n She performed in Japan on Saturday night before a flight across nine time zones and the international date line to reach the U.S.\\nRihanna performs during halftime of the NFL Super Bowl 57 football game between the Philadelphia Eagles and the Kansas City Chiefs, Sunday, Feb. 12, 2023, in Glendale, Ariz. (AP Photo/David J. Phillip)\\n After the teams take the field, Post Malone will perform “America the Beautiful” and Reba McEntire will sing “The Star-Spangled Banner.”\\nSan Francisco 49ers quarterback Brock Purdy (13) warms up before the NFL Super Bowl 58 football game against the Kansas City Chiefs, Sunday, Feb. 11, 2024, in Las Vegas. He was also the referee when the Chiefs beat the 49ers in the Super Bowl four years ago — and when the Rams beat the Saints in the 2019 NFC championship game after an infamous missed call.\\n Purdy’s comeback from the injury to his throwing arm suffered in last season’s NFC championship loss to the Philadelphia Eagles has been part of the storybook start to his career that started as Mr. Irrelevant as the 262nd pick in the 2022 draft.\\n'}]\n",
      "\u001b[32m***********************************************************************\u001b[0m\n",
      "\n",
      "--------------------------------------------------------------------------------\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/qingyunwu/.cache/chroma/onnx_models/all-MiniLM-L6-v2/onnx.tar.gz: 100%|██████████| 79.3M/79.3M [00:02<00:00, 33.3MiB/s]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[33mAssistant\u001b[0m (to user_proxy):\n",
      "\n",
      "Thought: The search results have provided detailed information on Super Bowl 2024, indicating that the Kansas City Chiefs won the Super Bowl in an overtime victory against the San Francisco 49ers with a final score of 25-22. This marks their third Super Bowl win in five years and their second consecutive title.\n",
      "\n",
      "Final Answer: The Kansas City Chiefs won Super Bowl 2024 with a 25-22 victory over the San Francisco 49ers in overtime.\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[33muser_proxy\u001b[0m (to Assistant):\n",
      "\n",
      "\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[33mAssistant\u001b[0m (to user_proxy):\n",
      "\n",
      "TERMINATE\n",
      "\n",
      "--------------------------------------------------------------------------------\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "ChatResult(chat_id=None, chat_history=[{'content': '\\nAnswer the following questions as best you can. You have access to tools provided.\\n\\nUse the following format:\\n\\nQuestion: the input question you must answer\\nThought: you should always think about what to do\\nAction: the action to take\\nAction Input: the input to the action\\nObservation: the result of the action\\n... (this process can repeat multiple times)\\nThought: I now know the final answer\\nFinal Answer: the final answer to the original input question\\n\\nBegin!\\nQuestion: What is the result of super bowl 2024?\\n', 'role': 'assistant'}, {'content': 'Thought: To find the result of Super Bowl 2024, I need to search for the most recent information available since the event is in the future as of my knowledge cutoff, and the result would only be known after the event takes place.\\n\\nAction: Use the provided search function to find the result of Super Bowl 2024.\\nAction Input: { query: \"Super Bowl 2024 result\" }', 'function_call': {'arguments': '{\"query\":\"Super Bowl 2024 result\"}', 'name': 'tavily_search_results_json'}, 'role': 'assistant'}, {'content': '[{\\'url\\': \\'https://www.espn.com/nfl/story/_/id/39480722/49ers-chiefs-live-super-bowl-lviii-updates-moments-highlights\\', \\'content\\': \"With a relentless defense and opportune plays by their star quarterback -- including a pair of gutsy overtime scrambles -- the Chiefs won their third Super Bowl in five years in a 25-22 overtime victory against the San Francisco 49ers in only the second overtime game in Super Bowl history.\\\\n Staff\\\\nTaylor Swift supports Travis Kelce, chugs drink at Super Bowl LVIII10hTory Barron\\\\nAfter posting a losing record in December, the Chiefs embraced an underdog, villain mentality throughout the postseason, upsetting three teams en route to their second consecutive Super Bowl title, becoming the ninth team to repeat as Super Bowl champions and first since the 2004 New England Patriots.\\\\n ESPN\\\\nSuper Bowl 2024 - Highlights from Chiefs\\' win vs. 49ers\\\\nMike Tannenbaum and Tim Hasselbeck react to the Chiefs\\' thrilling overtime victory over the 49ers in the Super Bowl. The 49ers had the ball with 2:51 to go in a tied game, but a signature Steve Spagnuolo blitz on third down late in the fourth quarter forced a 49ers field goal. Led by their captains, most of the Chiefs arrived to Allegiant Stadium in Las Vegas on Sunday in all black, signaling a steely resolve to upset Brock Purdy and the NFC\\'s best offensive ensemble.\\\\n\"}, {\\'url\\': \\'https://www.sportingnews.com/us/nfl/news/super-bowl-2024-live-score-49ers-chiefs-results-highlights/0c440aa7145b809ed174d8ff\\', \\'content\\': \\'Super Bowl start time\\\\nSuper Bowl 58 between the Chiefs and 49ers is set to kick off at 6:30 p.m. ET (3:30 p.m. local time) in Las Vegas, Nev.\\\\n6:30 p.m. ET has become the standard start time for Super Bowls and is preceded by performances of \"Lift Every Voice and Sing,\" \"America the Beautiful,\" and \"The Star-Spangled Banner. Brock Purdy drops a dime to Chris Conley on 3rd & 9 🎯\\\\n📺: #SBLVIII on CBS\\\\n📱: Stream on #NFLPlus https://t.co/dClcEDViWl pic.twitter.com/Oa10d7khdl\\\\n7:10 p.m. — The 49ers are relying heavily on McCaffrey, who takes the ball on each of the first three snaps on this drive. 📺: #SBLVIII on CBS\\\\n📱: Stream on #NFLPlus https://t.co/dClcEDViWl pic.twitter.com/yUc00MtP84\\\\n7:24 p.m. — The 49ers force a 3rd & 1, but Rashee Rice is easily able to pick up the first and one or two more yards.\\\\n49ers 3, Chiefs 0\\\\n7:19 p.m. MORE SUPER BOWL 58:\\\\n•\\\\xa0 Inside Taylor Swift and Travis Kelce\\\\\\'s whirlwind dating timeline\\\\n•. Ranking the thriving Mike and Kyle Shanahan coaching tree\\\\n•. Tracking every Super Bowl 58 commercial\\\\nWhat channel is the Super Bowl on?\\\\nSuper Bowl 58 will be broadcast nationally on CBS. PURDY TO JENNINGS TO CMC FOR SIX 🔥\\\\n📺: #SBLVIII on CBS\\\\n📱: Stream on #NFLPlus https://t.co/dClcEDViWl pic.twitter.com/ktiTXIiHzS\\\\n7:47 p.m. — L\\\\\\'Jarius Sneed, who was on the other side of this two weeks ago, gets hit with an unsportsmanlike conduct penalty and and gives the 49ers their 11th first down.\\'}, {\\'url\\': \\'https://www.nbcnews.com/news/sports/live-blog/super-bowl-chiefs-49ers-score-live-updates-rcna136833\\', \\'content\\': \"Profile\\\\nSections\\\\ntv\\\\nFeatured\\\\nMore From NBC\\\\nFollow NBC News\\\\nnews Alerts\\\\nThere are no new alerts at this time\\\\nSuper Bowl LVIII\\\\nSuper Bowl 2024 live updates: 49ers vs. Chiefs how to watch, kickoff time, pregame, Taylor Swift arrival\\\\nEverything you need to know about Super Bowl 58:\\\\nJason Abbruzzese\\\\nAnd we\\'re under way. “Despite the 12-hour flight and 17-hour time difference, the Embassy can confidently Speak Now to say that if she departs Tokyo in the evening after her concert, she should comfortably arrive in Las Vegas before the Super Bowl begins.”\\\\nNBC News\\\\nFootball fans across the country were celebrating after they were surprised with tickets to the Super Bowl in Las Vegas. Mahomes warms up\\\\nNBC News\\\\nICYMI: A person tried to climb the Vegas Sphere (and got arrested)\\\\nSaba Hamedy\\\\nLas Vegas police confirmed last Wednesday that a man was arrested after he climbed a structure on the 200 block of Sands Avenue, where the Sphere is located.\\\\n President Biden is skipping a Super Bowl interview\\\\nMonica Alba\\\\nJonathan Allen\\\\nFor the second year in a row, President Joe Biden is passing on the opportunity to sit down for a Super Bowl interview that could reach millions of Americans on Sunday — a move his advisers say is part of their larger communication strategy.\\\\n Can the Niners tie the Patriots and Steelers?\\\\nClaire Cardona\\\\nAllan Smith\\\\nThe Kansas City Chiefs, the reigning Super Bowl champs, could go two in a row if they beat the San Francisco 49ers.\\\\n\"}, {\\'url\\': \\'https://www.cbssports.com/nfl/news/2024-super-bowl-chiefs-vs-49ers-score-patrick-mahomes-leads-ot-comeback-as-k-c-wins-back-to-back-titles/live/\\', \\'content\\': \"The championship-winning drive, which included a fourth-and-1 scramble from Mahomes and a clutch 7-yard catch from tight end Travis Kelce, was a must-score for K.C. The NFL\\'s new playoff overtime rules -- both teams are guaranteed at least one possession in the extra period -- were in effect for the first time, and the Chiefs needed to answer the Niners\\' field goal.\\\\n Held out of the end zone until that point, Kansas City grabbed its first lead of the game at 13-10.\\\\nJennings\\' touchdown receiving (followed by a missed extra point) concluded a 75-yard drive that put the Niners back on top, 16-13, as the wideout joined former Philadelphia Eagles quarterback Nick Foles as the only players to throw and catch a touchdown in a Super Bowl.\\\\n He spread the ball around -- eight pass-catchers had at least two receptions -- slowly but surely overcoming a threatening 49ers defense that knocked him off his spot consistently in the first half.\\\\nMahomes, with his third Super Bowl MVP, now sits alongside Tom Brady (five) and Joe Montana (three) atop the mountain while becoming just the third player to win the award back-to-back, joining Bart Starr (I-II) and Terry Bradshaw (XIII-XIV).\\\\n The muffed punt that bounced off of cornerback Darrell Luter Jr.\\'s ankle was also the big break that the Chiefs needed as they scored on the very next play to take the lead for the first time in the game. College Pick\\'em\\\\nA Daily SportsLine Betting Podcast\\\\nNFL Playoff Time!\\\\n2024 Super Bowl, Chiefs vs. 49ers score: Patrick Mahomes leads OT comeback as K.C. wins back-to-back titles\\\\nCall it a dynasty; the Chiefs are the first team to win consecutive Super Bowls since 2003-04\\\\nThe Kansas City Chiefs are Super Bowl champions, again.\"}, {\\'url\\': \\'https://apnews.com/live/super-bowl-2024-updates\\', \\'content\\': \\'Throw in the fact that Chiefs coach Andy Reid will be in his fifth Super Bowl, the third most in NFL history, and has a chance to win a third ring, and the knowledge on the Kansas City sideline will be an advantage too big for the 49ers to overcome.\\\\n She performed in Japan on Saturday night before a flight across nine time zones and the international date line to reach the U.S.\\\\nRihanna performs during halftime of the NFL Super Bowl 57 football game between the Philadelphia Eagles and the Kansas City Chiefs, Sunday, Feb. 12, 2023, in Glendale, Ariz. (AP Photo/David J. Phillip)\\\\n After the teams take the field, Post Malone will perform “America the Beautiful” and Reba McEntire will sing “The Star-Spangled Banner.”\\\\nSan Francisco 49ers quarterback Brock Purdy (13) warms up before the NFL Super Bowl 58 football game against the Kansas City Chiefs, Sunday, Feb. 11, 2024, in Las Vegas. He was also the referee when the Chiefs beat the 49ers in the Super Bowl four years ago — and when the Rams beat the Saints in the 2019 NFC championship game after an infamous missed call.\\\\n Purdy’s comeback from the injury to his throwing arm suffered in last season’s NFC championship loss to the Philadelphia Eagles has been part of the storybook start to his career that started as Mr. Irrelevant as the 262nd pick in the 2022 draft.\\\\n\\'}]', 'name': 'tavily_search_results_json', 'role': 'function'}, {'content': 'Thought: The search results have provided detailed information on Super Bowl 2024, indicating that the Kansas City Chiefs won the Super Bowl in an overtime victory against the San Francisco 49ers with a final score of 25-22. This marks their third Super Bowl win in five years and their second consecutive title.\\n\\nFinal Answer: The Kansas City Chiefs won Super Bowl 2024 with a 25-22 victory over the San Francisco 49ers in overtime.', 'role': 'user'}, {'content': '', 'role': 'assistant'}, {'content': 'TERMINATE', 'role': 'user'}], summary='', cost=({'total_cost': 0.32121, 'gpt-4': {'cost': 0.32121, 'prompt_tokens': 9987, 'completion_tokens': 360, 'total_tokens': 10347}}, {'total_cost': 0.31053000000000003, 'gpt-4': {'cost': 0.31053000000000003, 'prompt_tokens': 9771, 'completion_tokens': 290, 'total_tokens': 10061}}), human_input=[])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from autogen.agentchat.contrib.capabilities import teachability\n",
    "\n",
    "# Instantiate the Teachability capability. Its parameters are all optional.\n",
    "teachability = teachability.Teachability(\n",
    "    verbosity=0,  # 0 for basic info, 1 to add memory operations, 2 for analyzer messages, 3 for memo lists.\n",
    "    reset_db=True,\n",
    "    path_to_db_dir=\"./tmp/notebook/teachability_db\",\n",
    "    recall_threshold=1.5,  # Higher numbers allow more (but less relevant) memos to be recalled.\n",
    ")\n",
    "\n",
    "# Now add the Teachability capability to the agent.\n",
    "teachability.add_to_agent(assistant)\n",
    "\n",
    "user_proxy.initiate_chat(\n",
    "    assistant,\n",
    "    message=react_prompt_message,\n",
    "    question=\"What is the result of super bowl 2024?\",\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4de68151",
   "metadata": {},
   "source": [
    "### Let's now ask the same question again to see if the assistant has remembered this."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "3e820661",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[33muser_proxy\u001b[0m (to Assistant):\n",
      "\n",
      "\n",
      "Answer the following questions as best you can. You have access to tools provided.\n",
      "\n",
      "Use the following format:\n",
      "\n",
      "Question: the input question you must answer\n",
      "Thought: you should always think about what to do\n",
      "Action: the action to take\n",
      "Action Input: the input to the action\n",
      "Observation: the result of the action\n",
      "... (this process can repeat multiple times)\n",
      "Thought: I now know the final answer\n",
      "Final Answer: the final answer to the original input question\n",
      "\n",
      "Begin!\n",
      "Question: What is the result of super bowl 2024?\n",
      "\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[33mAssistant\u001b[0m (to user_proxy):\n",
      "\n",
      "Thought: The user has given me a memory that directly answers the question about the result of Super Bowl 2024.\n",
      "Action: I will recall the information from the memory provided.\n",
      "Final Answer: The Kansas City Chiefs won Super Bowl 2024 with a 25-22 overtime victory against the San Francisco 49ers.\n",
      "\n",
      "--------------------------------------------------------------------------------\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "ChatResult(chat_id=None, chat_history=[{'content': '\\nAnswer the following questions as best you can. You have access to tools provided.\\n\\nUse the following format:\\n\\nQuestion: the input question you must answer\\nThought: you should always think about what to do\\nAction: the action to take\\nAction Input: the input to the action\\nObservation: the result of the action\\n... (this process can repeat multiple times)\\nThought: I now know the final answer\\nFinal Answer: the final answer to the original input question\\n\\nBegin!\\nQuestion: What is the result of super bowl 2024?\\n', 'role': 'assistant'}, {'content': 'Thought: The user has given me a memory that directly answers the question about the result of Super Bowl 2024.\\nAction: I will recall the information from the memory provided.\\nFinal Answer: The Kansas City Chiefs won Super Bowl 2024 with a 25-22 overtime victory against the San Francisco 49ers.', 'role': 'user'}], summary='Thought: The user has given me a memory that directly answers the question about the result of Super Bowl 2024.\\nAction: I will recall the information from the memory provided.\\nFinal Answer: The Kansas City Chiefs won Super Bowl 2024 with a 25-22 overtime victory against the San Francisco 49ers.', cost=({'total_cost': 0.37845, 'gpt-4': {'cost': 0.37845, 'prompt_tokens': 11623, 'completion_tokens': 496, 'total_tokens': 12119}}, {'total_cost': 0.34818000000000005, 'gpt-4': {'cost': 0.34818000000000005, 'prompt_tokens': 10886, 'completion_tokens': 360, 'total_tokens': 11246}}), human_input=[])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_proxy.initiate_chat(\n",
    "    assistant,\n",
    "    message=react_prompt_message,\n",
    "    question=\"What is the result of super bowl 2024?\",\n",
    "    max_turns=1,\n",
    ")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "flaml_dev",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
